package com.example.nutritionmanager.home

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import co.yml.charts.axis.AxisData
import co.yml.charts.common.model.Point
import co.yml.charts.ui.barchart.BarChart
import co.yml.charts.ui.barchart.models.BarChartData
import co.yml.charts.ui.barchart.models.BarData
import co.yml.charts.ui.barchart.models.BarStyle
import com.example.nutritionmanager.viewModels.IntakeViewModel
import java.lang.Math.ceil

@Composable
fun CalorieBarChart(
    intakeViewModel: IntakeViewModel = hiltViewModel(),
) {
    // 监听周数据
    val caloriesData by intakeViewModel.weekCalories.collectAsState()
    // 计算最大值（增加10%顶部空间）
    val maxValue = (caloriesData.maxOrNull() ?: 100).let {
        ceil(it * 1.1 / 100) * 100
    }.toFloat()

    val yAxisStep = (caloriesData.max() / 500 + 1) * 100

    Text(
        text = "最近七天卡路里摄入量",
        fontSize = 16.sp,
        fontWeight = FontWeight.Bold,
        color = MaterialTheme.colorScheme.onBackground,
        modifier = Modifier.padding(bottom = 8.dp)
    )

    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface, contentColor = MaterialTheme.colorScheme.surface),
        elevation = CardDefaults.cardElevation(4.dp),
    ) {
        Text(
            text = "单位：kcal",
            fontSize = 12.sp,
            color = MaterialTheme.colorScheme.onSurface,
            modifier = Modifier.padding(start = 16.dp, top = 16.dp)
        )
        BarChart(
            modifier = Modifier
                .clickable(enabled = false){}
                .fillMaxWidth()
                .height(200.dp)
                .background(MaterialTheme.colorScheme.surface)
                .padding(start = 8.dp, end = 8.dp, bottom = 12.dp),
            barChartData = BarChartData(
                backgroundColor = MaterialTheme.colorScheme.surface,
                chartData = (
                    caloriesData.mapIndexed { index, value ->
                        BarData(
                            color = MaterialTheme.colorScheme.secondary,
                            point = Point((index + 1).toFloat(), value.toFloat()),
//                            color = Color(0xFF4CAF50),
                            label = value.toString()
                        )
                    }
                ),
                xAxisData = AxisData.Builder()
                    .axisLabelFontSize(12.sp)
                    .backgroundColor(MaterialTheme.colorScheme.surface)
                    .axisLabelColor(MaterialTheme.colorScheme.onSurface) // X轴标签颜色
                    .axisLineColor(MaterialTheme.colorScheme.outline)
                    .labelAndAxisLinePadding(10.dp)// X轴线颜色
                    .labelData { index -> "${index + 1}天" }
                    .build(),
                yAxisData = AxisData.Builder()
                    .axisLabelFontSize(12.sp)
                    .backgroundColor(MaterialTheme.colorScheme.surface)
                    .axisLabelColor(MaterialTheme.colorScheme.onSurface) // Y轴标签颜色
                    .axisLineColor(MaterialTheme.colorScheme.outline)    // Y轴线颜色
                    .labelAndAxisLinePadding(10.dp)
                    .steps(5) // 5个主要刻度
                    .labelData { index -> "${yAxisStep * index}" }
                    .build(),
                barStyle = BarStyle(
                    paddingBetweenBars = 18.dp,
                    barWidth = 18.dp,
                ),
            )
        )
    }
}
@Preview
@Composable
fun CalorieBarChartPreview() {
    CalorieBarChart(
//        listOf(1000, 1200, 800, 1500, 1100, 900, 1300)
    )
}